<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/user/list' }">职工列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row>
      <el-table
        :data="users.slice((page-1)*pageSize,pageSize*page)"
        stripe
        style="width: 100%"
        class="tableinfo margintop20"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{'text-align':'center'}"
      >
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="success" @click="updateone(scope.row)">编辑</el-button>
            <el-popconfirm
              confirmButtonText="好的"
              cancelButtonText="不用了"
              icon="el-icon-info"
              iconColor="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="delUser(scope.row)"
            >
              <el-button size="mini" slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" sortable>
          <template slot-scope="scope">
            <el-tag
              effect="plain"
              :type="scope.row.status | colortype"
            >{{scope.row.status |statusname }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="roleid" sortable>
          <template slot-scope="scope">
            <el-tag
              effect="plain"
              :type="scope.row.roleid | colortype"
            >{{scope.row.roleid | rolename}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column label="手机号码" prop="mobile"></el-table-column>
        <el-table-column label="底薪" prop="money"></el-table-column>
        <el-table-column prop="entry" label="入职时间">
          <template slot-scope="scope">{{scope.row.entry | timeFormat("YYYY-MM-DD")}}</template>
        </el-table-column>
      </el-table>
    </el-row>



    <el-dialog title="修改用户" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form
        :model="form"
        :rules="rules"
        ref="myform"
        label-width="100px"
        label-position="right"
        class="myform"
      >
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleid">
          <el-select v-model="form.roleid">
            <el-option
              v-for="(role,i) in roleList"
              :key="i"
              :label="role.label"
              :value="role.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态：" prop="status" >
          <el-select v-model="form.status">
            <el-option
              v-for="(role,i) in statusList"
              :key="i"
              :label="role.label"
              :value="role.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input type="password" v-model="form.password" show-password></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateById()">确 定</el-button>
      </span>
    </el-dialog>
    <div class="flexbox">
          <el-pagination
            background
            layout="prev, pager, next"
            @current-change="changeCurrent"
            :page-size="pageSize"
            :current-page="page"
            :total="total"
          ></el-pagination>
          <span>共 {{total}} 条数据</span>
        </div>
  </div>
</template>

<script>
import { roleList, statusList } from "@/utils";
import { getuserlist, deluser, updateusername ,getuserlist1} from "@/api";
import Vue from "vue";
export default {
  data() {
    return {
      roleList,
      statusList,
      page:1,
      pageSize:6,
      form: {},
      users: [],
      dialogVisible: false,
      rules: {
        // 表单校验   正则
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            validator: function (rule, value, callback) {
              var mobileReg = /^1(3|5|6|7|8|9)\d{9}$/;
              if (mobileReg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          //失去焦点触发
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            validator: function (rule, value, callback) {
              var passReg = /^[A-Za-z0-9]\w{3,12}$/;
              if (value.length < 4) {
                callback(new Error("密码长度至少4位"));
              } else if (value.length > 12) {
                callback(new Error("密码长度最多12位"));
              } else if (!passReg.test(value)) {
                callback(new Error("请输入正确格式的密码"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        roleid: [
          { required: true, message: "请选择用户角色类型", trigger: "change" },
        ],
        username: [{ required: true, message: "请输入昵称", trigger: "blur" }],
      },
    };
  },
  computed: {
    total() {
      return this.users.length;
    },
  },
  methods: {
    changeCurrent(page) {
      this.page = page;
    },
    adduser(){
      this.$router.push({name:useradd})
    },
    updateById() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          var roleid = sessionStorage.getItem("roleid");
          console.log(roleid);
          if (roleid < this.form.roleid) {
            this.$message.error("你的权限较低，无法修改...");
          } else {
            updateusername(this.form).then((res) => {
              if (res.type) {
                this.dialogVisible = false;
                this.users.forEach((item, i) => {
                  if (item._id == this.form._id) {
                    Vue.set(this.users, i, this.form);
                  }
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    updateone(row) {
      var roleid = sessionStorage.getItem("roleid");
      if (roleid < row.roleid) {
        this.$message.error("你权限不够，没资格操作..");
      } else {
        (this.dialogVisible = true), (this.form = { ...this.form, ...row });
      }
    },
    delUser(row) {
      var mobile = localStorage.getItem("mobile");
      var roleid = sessionStorage.getItem("roleid");
      if (roleid < row.roleid) {
        this.$message.error("你权限不够，没资格删除..");
      } else if (mobile == row.mobile) {
        this.$message.error("你无法删除自己..");
      } else {
        deluser({
          _id: row._id,
        }).then((res) => {
          if (res.type) {
            this.users = this.users.filter((item) => item._id !== row._id);
          }
        });
      }
    },
  },
  mounted() {
    getuserlist().then((res) => {
      if (res.type) {
      // this.users =[ ...res.result,...res.data];
      this.users = res.result
      // console.log(this.users);
      }
    });
  },
};
</script>


